package com.qf.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qf.dao.PetMapper;
import com.qf.dao.PlaneOrderMapper;
import com.qf.dao.PlaneSeatMapper;
import com.qf.pojo.po.*;
import com.qf.pojo.req.POrderReq;
import com.qf.pojo.resp.BaseResp;
import com.qf.pojo.resp.PlaneOrderResp;
import com.qf.service.PlaneSeatService;
import com.qf.utils.GenerateOrderNum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Api(value = "飞机订单")
@RestController
@RequestMapping(value = "/planeOrder")
public class PorderController {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    PetMapper petMapper;
    @Autowired
    PlaneSeatMapper planeSeatMapper;
    @Autowired
    PlaneSeatService planeSeatService;
    @Autowired
    PlaneOrderMapper planeOrderMapper;

    //生成订单
    @ApiOperation("生成飞机订单")
    @RequestMapping(value = "/addOrder",method = RequestMethod.POST)
    @Transactional
    public BaseResp addOrder(@RequestBody POrderReq orderReq, HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("token");
        Object token1 = redisTemplate.opsForValue().get(token);
        XcUser planeUser = JSONObject.parseObject(JSONObject.toJSONString(token1), XcUser.class);

        if (planeUser==null){
            return new BaseResp(1,"该用户登录已过期，请重新登录！",null,null);
        }

        //生成父类订单
        String generate = GenerateOrderNum.generate("");
        System.out.println(generate);
        //创建Order属性
        PlaneOrder planeOrder = new PlaneOrder();
        planeOrder.setOrderId(generate); //订单号
        planeOrder.setOrderPay(orderReq.getSeatprice()); // 订单
        planeOrder.setOrderStatus(1); // 订单状态
        planeOrder.setOrderType(2); //
        planeOrder.setUserId(planeUser.getId()); //订单用户idw
        planeOrder.setCreateTime(new Date());
        System.out.println(planeOrder);
//        xcOrder.setCreateTime(new Date()); // 订单 时间
        //批量添加入数据库
        planeOrderMapper.insertOrder(planeOrder);

       //子类订单
        String generate1 = GenerateOrderNum.generate("");
        PlaneOrderDetail planeOrderDetail = new PlaneOrderDetail();
        planeOrderDetail.setPlaneId(orderReq.getPlaneid());
        planeOrderDetail.setOrderDetailId(generate1);
        planeOrderDetail.setCreateTime(new Date());
        planeOrderDetail.setOrderId(planeOrder.getOrderId());
        //出发地,离开地
        planeOrderDetail.setDispatureId(orderReq.getDepartureid());
        planeOrderDetail.setDestinationId(orderReq.getDestination());
        System.out.println(planeOrderDetail);
        //批量添加数据库
        petMapper.insertOrderDetail(planeOrderDetail);

        return  new BaseResp(0,"订单生成成功",null,null);
    }
    //查询订单
    @ApiOperation("查询未支付的机票订单")
    @RequestMapping(value = "/findXcOrderById",method = RequestMethod.GET)
    public BaseResp findXcOrderById(HttpServletRequest request) {
        String token = request.getHeader("token");
        Object token1 = redisTemplate.opsForValue().get(token);
        XcUser planeUser = JSONObject.parseObject(JSONObject.toJSONString(token1), XcUser.class);

        if (planeUser==null){
            return new BaseResp(1,"该用户登录已过期，请重新登录！",null,null);
        }

        List<PlaneOrderResp> list = petMapper.findXcOrderById(planeUser.getId());
        System.out.println(planeUser.getId());
        System.out.println(list);
        return new BaseResp(0,"订单查询成功",null,list);

    }

    //查询订单
    @ApiOperation("查询所有的机票订单")
    @RequestMapping(value = "/findXcOrderByIdAll",method = RequestMethod.GET)
    public BaseResp findXcOrderByIdAll(HttpServletRequest request) {
        String token = request.getHeader("token");
        Object token1 = redisTemplate.opsForValue().get(token);
        XcUser planeUser = JSONObject.parseObject(JSONObject.toJSONString(token1), XcUser.class);

        if (planeUser==null){
            return new BaseResp(1,"该用户登录已过期，请重新登录！",null,null);
        }

        List<PlaneOrderResp> list = petMapper.findXcOrderByIdAll(planeUser.getId());
        System.out.println(planeUser.getId());
        System.out.println(list);
        return new BaseResp(0,"订单查询成功",null,list);
    }

    //删除订单,取消订单
    @RequestMapping("/deleteorder")
    public BaseResp deleteOrder(@RequestBody Map map){
//        System.out.println(map);
//        //订单id
//        Object orderId = map.get("orderId");
//        Object o = map.get("xcOrderDetail");
//
//        //转对象
//        String s = JSON.toJSONString(o);
//        XcOrderDetail xcOrderDetail = JSONObject.parseObject(s, XcOrderDetail.class);
//        String orderDetailId = xcOrderDetail.getOrderDetailId();
//        String planeId = xcOrderDetail.getPlaneId();

        //删除父订单

        //xcOrderDetail对象中有orderDetailId,也有planeId;
        return new BaseResp(0,"null",null,null);

    }

}
